home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / gcc / ixemlsrc.lha / ixemul / ixnet / getttyent.c < prev    next >
C/C++ Source or Header  |  1996-03-13  |  5KB  |  199 lines

  1. /*
  2.  * Copyright (c) 1989 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #if defined(LIBC_SCCS) && !defined(lint)
  35. static char sccsid[] = "@(#)getttyent.c 5.10 (Berkeley) 3/23/91";
  36. #endif /* LIBC_SCCS and not lint */
  37.  
  38. #include <sys/types.h>
  39. #include <ttyent.h>
  40. #include <stdio.h>
  41. #include <string.h>
  42. #include "utils.h"
  43.  
  44. static char zapchar;
  45. static FILE *tf;
  46.  
  47. struct ttyent *
  48. getttynam(const char *tty) {
  49.     register struct ttyent *t;
  50.  
  51.     setttyent();
  52.     while ((t = getttyent()))
  53.     if (!strcmp(tty, t->ty_name))
  54.         break;
  55.     endttyent();
  56.     return (t);
  57. }
  58.  
  59. #define MAXTTYLINELENGTH   100
  60.  
  61. struct ttyent *
  62. getttyent(void) {
  63.     static struct ttyent tty;
  64.     register int c;
  65.     register char *p;
  66.     static char line[MAXTTYLINELENGTH];
  67.     static char *skip(), *value();
  68.  
  69.     if (!tf && !setttyent())
  70.     return (NULL);
  71.  
  72.     for (;;) {
  73.     if (!fgets(p = line, sizeof(line), tf))
  74.         return (NULL);
  75.  
  76.     /* skip lines that are too big */
  77.     if (!index(p, '\n')) {
  78.         while ((c = getc(tf)) != '\n' && c != EOF)
  79.             ;
  80.         continue;
  81.     }
  82.  
  83.     while ((isspace(*p)))
  84.         ++p;
  85.  
  86.     if (*p && *p != '#')
  87.         break;
  88.     }
  89.  
  90.     zapchar = 0;
  91.     tty.ty_name = p;
  92.     p = skip(p);
  93.     if (!*(tty.ty_getty = p))
  94.         tty.ty_getty = tty.ty_type = NULL;
  95.     else {
  96.     p = skip(p);
  97.     if (!*(tty.ty_type = p))
  98.         tty.ty_type = NULL;
  99.     else
  100.         p = skip(p);
  101.     }
  102.     tty.ty_status = 0;
  103.     tty.ty_window = NULL;
  104.  
  105. #define scmp(e) !strncmp(p, e, sizeof(e) - 1) && isspace(p[sizeof(e) - 1])
  106. #define vcmp(e) !strncmp(p, e, sizeof(e) - 1) && p[sizeof(e) - 1] == '='
  107.     for (; *p; p = skip(p)) {
  108.     if (scmp(_TTYS_OFF))
  109.         tty.ty_status &= ~TTY_ON;
  110.     else if (scmp(_TTYS_ON))
  111.         tty.ty_status |= TTY_ON;
  112.     else if (scmp(_TTYS_SECURE))
  113.         tty.ty_status |= TTY_SECURE;
  114.     else if (vcmp(_TTYS_WINDOW))
  115.         tty.ty_window = value(p);
  116.     else
  117.         break;
  118.     }
  119.  
  120.     if (zapchar == '#' || *p == '#')
  121.     while ((c = *++p) == ' ' || c == '\t')
  122.         ;
  123.     tty.ty_comment = p;
  124.     if (*p == 0)
  125.     tty.ty_comment = 0;
  126.  
  127.     p = index(p, '\n');
  128.     if (p)
  129.     *p = '\0';
  130.  
  131.     return (&tty);
  132. }
  133.  
  134. #define QUOTED    1
  135.  
  136. /*
  137.  * Skip over the current field, removing quotes, and return a pointer to
  138.  * the next field.
  139.  */
  140. static char *
  141. skip(char *p) {
  142.     register char *t;
  143.     register int c, q;
  144.  
  145.     for (q = 0, t = p; (c = *p) != '\0'; p++) {
  146.     if (c == '"') {
  147.         q ^= QUOTED;    /* obscure, but nice */
  148.         continue;
  149.     }
  150.     if (q == QUOTED && *p == '\\' && *(p+1) == '"')
  151.         p++;
  152.     *t++ = *p;
  153.     if (q == QUOTED)
  154.         continue;
  155.     if (c == '#') {
  156.         zapchar = c;
  157.         *p = 0;
  158.         break;
  159.     }
  160.     if (c == '\t' || c == ' ' || c == '\n') {
  161.         zapchar = c;
  162.         *p++ = 0;
  163.         while ((c = *p) == '\t' || c == ' ' || c == '\n')
  164.         p++;
  165.         break;
  166.     }
  167.     }
  168.     *--t = '\0';
  169.     return (p);
  170. }
  171.  
  172. static char *
  173. value(char *p) {
  174.     return ((p = index(p, '=')) ? ++p : NULL);
  175. }
  176.  
  177. int
  178. setttyent(void) {
  179.     if (tf) {
  180.     (void)rewind(tf);
  181.     return (1);
  182.     }
  183.     else if ((tf = fopen(_PATH_TTYS, "r")))
  184.     return (1);
  185.     return (0);
  186. }
  187.  
  188. int
  189. endttyent(void) {
  190.     int rval;
  191.  
  192.     if (tf) {
  193.     rval = !(fclose(tf) == EOF);
  194.     tf = NULL;
  195.     return (rval);
  196.     }
  197.     return (1);
  198. }
  199.